package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysCatNurtureRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysCatNurtureMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysCatNurture;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysCatNurtureService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 猫咪养护业务层处理
 *
 * @author mars
 * @date 2024-03-29
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysCatNurtureServiceImpl implements ISysCatNurtureService {

    private final SysCatNurtureMapper baseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysCatNurture add(SysCatNurtureRequest request) {
        SysCatNurture entity = SysCatNurture.builder().build();
        BeanUtils.copyProperties(request, entity);
        Long userId = ContextUserInfoThreadHolder.get().getId();
        entity.setCreateById(userId);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysCatNurtureRequest request) {
        SysCatNurture entity = SysCatNurture.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysCatNurture getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<SysCatNurture> pageList(SysCatNurtureRequest request) {
        Page<SysCatNurture> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysCatNurture> query = this.buildWrapper(request);
        IPage<SysCatNurture> pageRecord = baseMapper.selectPage(page, query);
        List<SysCatNurture> records = pageRecord.getRecords();
        List<SysCatNurture> recordsNew = new ArrayList<>();
        for (SysCatNurture nurture : records) {
            if (nurture.getContent().length() > 200) {
                nurture.setContent(nurture.getContent().substring(0, 200));
            }
            recordsNew.add(nurture);
        }
        pageRecord.setRecords(recordsNew);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysCatNurture> list(SysCatNurtureRequest request) {
        LambdaQueryWrapper<SysCatNurture> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysCatNurture> buildWrapper(SysCatNurtureRequest param) {
        LambdaQueryWrapper<SysCatNurture> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getTitle())) {
            query.like(SysCatNurture::getTitle, param.getTitle());
        }
        if (StringUtils.isNotBlank(param.getContent())) {
            query.like(SysCatNurture::getContent, param.getContent());
        }
        return query;
    }

}
